<template>
  <el-dialog title="生成兑换券" :visible.sync="visible" :close-on-click-modal="false" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-position="left" :label-width="formLabelWidth">
      <el-form-item label="请选择元素" prop="element">
        <el-checkbox-group v-model="form.element">
          <el-checkbox label="1">数字</el-checkbox>
          <el-checkbox label="2">大写字母</el-checkbox>
          <el-checkbox label="3">小写字母</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="兑换码前缀" prop="isPrefix">
        <el-switch v-model="form.isPrefix"></el-switch>
      </el-form-item>
      <el-form-item label="" prop="prefix">
        <el-input v-if="form.isPrefix" v-model="form.prefix" style="width: 100px;"></el-input>
      </el-form-item>
      <el-form-item label="请输入长度" prop="length">
        <el-input-number v-model="form.length" :min="10"></el-input-number>
        位字符
      </el-form-item>
      <el-form-item label="有效期" prop="validityPeriod">
        <el-input-number v-model="form.validityPeriod" :min="1"></el-input-number>
        天
      </el-form-item>
      <el-form-item label="生成数量" prop="num">
        <el-input-number v-model="form.num" :min="1"></el-input-number>
        个
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
        <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
<!--        <el-button @click="resetForm('form')">重置</el-button>-->
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {exchangevoucherGenerate} from "@/api/exchange/exchangevoucher.js";

export default {
  data() {
    return {
      visible: false,
      form: {
        element: [],
        isPrefix: true,
        prefix: "",
        length: "",
        validityPeriod: 0,
        num: 1,
      },
      formLabelWidth: "100px",
      rules: {
        length: [
          {required: true, message: "请输入长度", trigger: "blur"},
        ],
        validityPeriod: [
          {required: true, message: "请输入有效期", trigger: "blur"},
        ],
        num: [
          {required: true, message: "请输入生成数量", trigger: "blur"},
        ],
      },
      CodeType_exchangeList: [],
      YesNoList: [],
      VoucherStatus_exchangeList: [],
      exchangeIdList: [],
      memberIdList: [],
      set: {},
    };
  },
  created() {
  },
  methods: {
    init(set) {
      this.set = set
      this.resetForm();
      this.visible = true;
    },
    //提交表单数据
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.exchangeId = this.set.id
          this.form.element = this.form.element.join(",")
          exchangevoucherGenerate(this.form).then((resp) => {
            if (resp.code == 0) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.$emit("refreshData");
              this.visible = false;
            } else {
              this.$msg({
                message: resp.msg,
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.form = {
        element: [],
        isPrefix: true,
        prefix: "",
        length: "",
        validityPeriod: 0,
        num: 1,
      };
      this.$nextTick(() => {
        if (this.$refs["form"]) {
          this.$refs["form"].resetFields();
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 274px;
  padding: 7px;
  overflow: hidden;
  overflow-y: auto;
  font-weight: normal;
}
</style>
